package com.example.adouble.cehuamenu.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.adouble.cehuamenu.R;
import com.example.adouble.cehuamenu.mode.User;
import com.example.adouble.cehuamenu.util.PathGetter;

import java.io.File;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 个人信息
 */

public class InfoActivity extends AppCompatActivity {

    private ImageView info_back;
    private CircleImageView uicon;
    private LinearLayout uhead;
    private File file;
    private BmobFile bmobFile;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);

        initView();
        sp = getSharedPreferences("login",MODE_PRIVATE);

        info_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //选取头像
        uhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent toGallery = new Intent(Intent.ACTION_GET_CONTENT);
                toGallery.addCategory(Intent.CATEGORY_OPENABLE);
                toGallery.setType("image/*");
                startActivityForResult(toGallery,1);
            }
        });

    }

    /*
    *返回
     */
    public void exit(View v){
        clearInfo();
        finish();
    }

    /*
    *初始化
     */
    public void initView(){
        info_back = findViewById(R.id.info_back);
        uicon = findViewById(R.id.u_icon);
        uhead = findViewById(R.id.u_head);
    }

    /*
    *清除登陆信息
     */
    public void clearInfo(){
        SharedPreferences clean = getSharedPreferences("login",MODE_PRIVATE);
        SharedPreferences.Editor ed = clean.edit();
        ed.clear();
        ed.commit();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null){return;}
        if(requestCode == 1){
            //获取图片的地址
            final Uri uri = data.getData();
            file = new File(PathGetter.getPath(this,uri));
            bmobFile = new BmobFile(file);
            bmobFile.uploadblock(new UploadFileListener() {
                @Override
                public void done(BmobException e) {
                    if(e == null){
                        final BmobQuery<User> bmobQuery = new BmobQuery<>();
                        bmobQuery.findObjects(new FindListener<User>() {
                            @Override
                            public void done(List<User> list, BmobException e) {
                                User user = new User();
                                user.setIcon(bmobFile);
                                user.setPath(bmobFile.getFileUrl());
                                user.update(sp.getString("id",null), new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        Glide.with(InfoActivity.this).load(uri).into(uicon);
                                    }
                                });
                            }
                        });
                        Toast.makeText(InfoActivity.this,"头像上传成功",Toast.LENGTH_SHORT).show();

                    }else{
                        Toast.makeText(InfoActivity.this,"上传失败",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        BmobQuery<User> bmobQuery = new BmobQuery<>();
        bmobQuery.getObject(sp.getString("id",null), new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                Glide.with(InfoActivity.this).load(user.getPath()).into(uicon);
            }
        });
    }
}
